package com.lottery.ruok.lottrey.chat.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ChooseManagerActivity;
import com.lottery.ruok.lottrey.activity.NewChannelInfoActivity;
import com.lottery.ruok.lottrey.chat.RongCloudChatManager;
import com.lottery.ruok.lottrey.chat.event.manager.UnReadMessageManager;
import com.lottery.ruok.lottrey.chat.model.redpacket.message.receiveRedPacketMessage.ReceiveRedPacketMessage;
import com.lottery.ruok.lottrey.chat.model.system.SystemMessage;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imkit.manager.IUnReadMessageObserver;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import utils.StringUtils;
import utils.Utils;

/**
 * 聊天室titleBar
 */
public class ChatRoomTitleBar extends BaseRelativeLayout {
    @BindView(R.id.tv_chatroom_title)
    TextView tvChatroomTitle;
    @BindView(R.id.tv_chatroom_manager)
    TextView tvChatroomManager;
    @BindView(R.id.tv_bubble)
    TextView tvBubble;
    @BindView(R.id.view_bubble)
    RelativeLayout viewBubble;
    @BindView(R.id.tv_chatroom_kefu)
    TextView tvChatroomKefu;
    @BindView(R.id.my_toolbar)
    LinearLayout myToolbar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private ChatRoomTitleModel data;
    private IUnReadMessageObserver iUnReadMessageObserver;
    private MessageContent messageContent;
    private String targetId;
    private String targetName;

    public ChatRoomTitleBar(Context context) {
        super(context);
    }

    public ChatRoomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChatRoomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(final Context context) {
        setContentView(R.layout.view_chat_room_title_bar);
        iUnReadMessageObserver = new IUnReadMessageObserver() {
            @Override
            public void onCountChanged(int i) {
                if (data != null) {
                    data.setUnReadCount(i);
                    updateBubbleView();
                }
            }
        };


    }



    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        UnReadMessageManager.getInstance().registerPrivateUnReadMessageCountChangedObserver(iUnReadMessageObserver);
    }

    @Override
    protected void onDetachedFromWindow() {
        UnReadMessageManager.getInstance().unRegisterPrivateUnReadMessageCountChangedObserver(iUnReadMessageObserver);
        super.onDetachedFromWindow();
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(ChatRoomTitleModel data) {
        this.data = data;
        updateView();
    }

    private void updateView() {
        if (data != null) {
            if (data.getChatType() == Conversation.ConversationType.CHATROOM) {
                if (StringUtils.isEmpty(UserInfo.getToken())) {
                    //      tvChatroomTitle.setText(data.getTitle() + "（" + data.getChatUserNum() + "）");
                    tvChatroomTitle.setText(data.getTitle());
                }else {
                    tvChatroomTitle.setText(data.getTitle() + "（" + data.getChatUserNum() +"人"+ "）");
                }
                viewBubble.setVisibility(VISIBLE);
            } else {
                tvChatroomTitle.setText(data.getTitle());
                viewBubble.setVisibility(GONE);
                tvChatroomKefu.setVisibility(GONE);
            }
            if (data.getRule() == 1) {
                //普通会员
                tvChatroomManager.setText("管理员");
            } else {
                tvChatroomManager.setText("群成员");
            }
            updateBubbleView();
        }
    }

    private void updateBubbleView() {
        if (data != null) {
            if (data.getUnReadCount() <= 0) {
                tvBubble.setVisibility(GONE);
            } else {
                tvBubble.setVisibility(VISIBLE);
            }
            tvBubble.setText(data.getUnReadCount() + "");
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_chatroom_manager, R.id.tv_chatroom_kefu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ((Activity) mContext).finish();
                break;
            case R.id.tv_chatroom_manager:
                if (data != null) {
                    if (data.getRule() == 1) {
                        // 选择管理员界面
                        Intent intent = new Intent(mContext, ChooseManagerActivity.class);
                        mContext.startActivity(intent);
                        ((Activity) mContext).overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
                    } else {
                        // 查看群成员
                        Intent intent = new Intent(mContext, NewChannelInfoActivity.class);
                        mContext.startActivity(intent);
                    }
                }
                break;
            case R.id.tv_chatroom_kefu:
                // 调整到客服页面
                Utils.openBrowser(Common.service_url, mContext);
        }


    }


    public static class ChatRoomTitleModel {
        /**
         * 房间名称
         */
        private String title;
        /**
         * 房间角色（仅用于聊天室）
         */
        private int rule;
        /**
         * 房间类型（融云）
         */
        private Conversation.ConversationType chatType;
        /**
         * 会话id
         */
        private String targetId;
        /**
         * 房间人数
         */
        private int chatUserNum;
        /**
         * 未读消息数
         */
        private int unReadCount;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getRule() {
            return rule;
        }

        public void setRule(int rule) {
            this.rule = rule;
        }

        public Conversation.ConversationType getChatType() {
            return chatType;
        }

        public void setChatType(Conversation.ConversationType chatType) {
            this.chatType = chatType;
        }

        public String getTargetId() {
            return targetId;
        }

        public void setTargetId(String targetId) {
            this.targetId = targetId;
        }

        public int getChatUserNum() {
            return chatUserNum;
        }

        public void setChatUserNum(int chatUserNum) {
            this.chatUserNum = chatUserNum;
        }

        public int getUnReadCount() {
            return unReadCount;
        }

        public void setUnReadCount(int unReadCount) {
            this.unReadCount = unReadCount;
        }
    }
}
